package objaction;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URISyntaxException;

import com.ardor3d.example.ExampleBase;
import com.ardor3d.extension.model.obj.ObjGeometryStore;
import com.ardor3d.extension.model.obj.ObjImporter;
import com.ardor3d.math.Vector3;
import com.ardor3d.util.resource.ResourceLocatorTool;
import com.ardor3d.util.resource.SimpleResourceLocator;

/**
 * Simplest example of loading a Wavefront OBJ model.
 */

public class App extends ExampleBase {
	public static void main(final String[] args) throws Exception {
		EmbededLGWLSupport.loadEmbededLGWGL();
		ExampleBase.start(App.class);
	}

	@Override
	protected void initExample() {

		_canvas.setTitle("Ardor3D - Simple Obj Example");
		_canvas.getCanvasRenderer().getCamera().setLocation(new Vector3(0, 5, 20));

		// Load the collada scene
		final long time = System.currentTimeMillis();
		final ObjImporter importer = new ObjImporter();
		SimpleResourceLocator resource = null;
		try {
			resource = new SimpleResourceLocator(
					ResourceLocatorTool.getClassPathResource(App.class, "media/models/obj"));
			importer.setTextureLocator(resource);
		} catch (final URISyntaxException ex) {
			ex.printStackTrace();
		}

		final ObjGeometryStore storage = importer.load("/obj/pitcher.obj");
		System.out.println("Importing Took " + (System.currentTimeMillis() - time) + " ms");

		/*
		 * Extract the 3D data
		 */
		// Get the correct Line Separator for the OS (CRLF or LF)
		final String nl = System.getProperty("line.separator");
		StringBuffer sb = new StringBuffer();
		BufferedWriter output = null;

		sb.append("int kadNumVertices = " + Integer.toString(storage.getDataStore().getVertices().size()) + ";");
		sb.append(nl);
		sb.append(nl);

		sb.append("float kadNormals [] = {");
		for (final Vector3 vector : storage.getDataStore().getGeneratedNormals()) {
			if (vector != null) {
				sb.append(Float.toString(vector.getXf()) + ",");
				sb.append(Float.toString(vector.getYf()) + ",");
				sb.append(Float.toString(vector.getZf()) + ",");
			}
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append("};");
		sb.append(nl);
		sb.append(nl);

		sb.append("float kadVertices [] = {");
		for (final Vector3 vector : storage.getDataStore().getVertices()) {
			if (vector != null) {
				sb.append(Float.toString(vector.getXf()) + ",");
				sb.append(Float.toString(vector.getYf()) + ",");
				sb.append(Float.toString(vector.getZf()) + ",");
			}

		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append("};");
		sb.append(nl);
		sb.append(nl);

		sb.append("float kadTextureCoordinates [] = {");
		for (final Vector3 vector : storage.getDataStore().getUvs()) {
			if (vector != null) {
				sb.append(Float.toString(vector.getXf()) + ",");
				sb.append(Float.toString(vector.getYf()) + ",");
				sb.append(Float.toString(vector.getZf()) + ",");
			}
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append("};");
		sb.append(nl);
		sb.append(nl);

		// print out the 3D data to the file
		try {
			output = new BufferedWriter(new FileWriter("kad.h"));
			output.write(sb.toString());
			output.close();
			System.out.println("FINISHED!");
		} catch (final IOException ex) {
			// TODO Auto-generated catch block
			ex.printStackTrace();
		}

		_root.attachChild(storage.getScene());
	}

}
